var cgjsd = {
    setBtnStatus: function () {
        var row = $('#grid_cgjsd').datagrid('getSelected');
        if (row && row.id && row.zt) {
            if (row.zt == 0 || row.zt == 1) {
                $("#btn_cgjsd_mod").show();
                $("#btn_cgjsd_del").show();
                $("#btn_cgjsd_check").show();
            } else if (row.zt == 2 || row.zt == 3) {
                $("#btn_cgjsd_mod").hide();
                $("#btn_cgjsd_del").hide();
                $("#btn_cgjsd_check").hide();
            }
        } else {
            $("#btn_cgjsd_mod").hide();
            $("#btn_cgjsd_del").hide();
            $("#btn_cgjsd_check").hide();
        }
    },
    alwayShowDetail: function (rowData) {
        $('#layout_cgjsd').layout('remove', 'center');
        $('#layout_cgjsd').layout('add', {
            region: 'center',
            split: true,
            href: $.webPath() + '/jsgl/cgjsd/cgjsd_detail?cgjsdId=' + rowData.id
        });
    },
    showDetail: function (rowData) {
        if (rowData && $("#cgjsd_detail_id").val() != rowData.id) {
            cgjsd.alwayShowDetail(rowData);
        }
    }


}
$(function () {
    //表格初始加载
    $('#grid_cgjsd').datagrid({
        url: $.webPath() + "/jsgl/cgjsd/cgjsdList",
        toolbar: "#toolbar_cgjsd",
        fitColumns: true,
        singleSelect: true,
        pagination: true,
        pageNumber: 1,
        fit: true,
        rownumbers: true,
        striped: true,
        columns: [
            [
                {field: 'jsdh', title: '结算单号', align: 'center', width: 113, formatter: cgjsdCell},
                {field: 'sqrq', title: '申请日期', align: 'center', width: 70},
                {field: 'type', title: '类型', align: 'center', width: 34,
                    formatter: function (value) {
                        if (value == '0')
                            return '进项';
                        if (value == '1')
                            return '销售';
                    }
                },
                {field: 'glqy_qc', title: '供应商名称', align: 'center', width: 119},
                {field: 'glqy_qyfr_nsh', title: '纳税号', align: 'center', width: 89},
                {field: 'fpze', title: '发票总额(元)', align: 'right', width: 136,
                    formatter: function (value) {
                        var sl = formatJeForHtml2(value, ",");
                        return sl;
                    }
                },
                {field: 'ddfy', title: '代垫运费(元)', align: 'right', width: 102,
                    formatter: function (value) {
                        var sl = formatJeForHtml2(value, ",");
                        return sl;
                    }
                },
                {field: 'fpzl', title: '发票种类', align: 'center', width: 44,
                    formatter: function (value) {
                        if (value == '0')
                            return '增票';
                        if (value == '1')
                            return '普票';
                        if (value == '2')
                            return '其他';
                        if (value == '3')
                            return '另开票';
                    }
                },
                {field: 'hm', title: '发票号码', align: 'center', width: 85},
                {field: 'sqr_xm', title: '申请人', align: 'center', width: 78},
                {field: 'gs_jc', title: '申报公司', align: 'center', width: 78},
                {field: 'cggs_jc', title: '采购公司', align: 'center', width: 78},
                {field: 'fpthyy', title: '退回原因', align: 'center', width: 51},
                {field: 'bz', title: '备注', align: 'center', width: 53},
                {field: "zt", title: "状态", align: 'center', width: 47,
                    formatter: function (value) {
                        if (value == '0')
                            return '未提交';
                        if (value == '1')
                            return '已退回';
                        if (value == '2')
                            return '已提交';
                        if (value == '3')
                            return '已审核';
                    }}

            ]
        ],
        onLoadSuccess: function () {
            $(this).datagrid('selectRow', 0);
            cgjsd.setBtnStatus();

            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        onClickRow: function () {
            cgjsd.setBtnStatus();
        },
        onSelect: function (rowIndex, rowData) {
            cgjsd.setBtnStatus();
        }
    });

    $("#cgjsd_glqy_search").qyjgzcombobox();
    //**********按钮触发事件**********
    //新增按钮
    $("#btn_cgjsd_add").click(function () {
        cgjsd_add('新增结算单', 'add_cgjsd');
    });
    //修改按钮
    $("#btn_cgjsd_mod").click(function () {
        cgjsd_modify('修改结算单', 'update_cgjsd');
    });
    //删除按钮
    $("#btn_cgjsd_del").click(function () {
        cgjsd_delete();
    });
    // 审核按钮
    $("#btn_cgjsd_check").click(function () {
        cgjsd_check();
    });
    //查询按钮
    $("#btn_cgjsd_search").click(function () {
        cgjsd_search();
    });
    //双击事件
    $("#grid_cgjsd").datagrid({
        onDblClickCell: function (index, field) {
            if (field == "jsdh") {
                $("#btn_cgjsd_mod:visible").click();
//                cgjsd_modify('修改结算单', 'update_cgjsd');
            }
        }
    });
    //加载查询条件中的申报公司下拉框
    $("#cgjsdinto_gs").combotree({
        loadFilter: function (rows) {
            return convert(rows);
        }
    });
});

//新增采购结算单
function cgjsd_add(title, action) {
    var href = $.webPath() + "/jsgl/cgjsd/cgjsd_content";
    var actionUrl = $.webPath() + "/jsgl/cgjsd/add_cgjsd";
    cgjsd_showDialog(title, href, actionUrl);
}

//修改采购结算单
function cgjsd_modify(title, action) {
    var record = $("#grid_cgjsd").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要更新的记录!", "warning");
        return;
    }
    var href = $.webPath() + "/jsgl/cgjsd/cgjsd_content?cgjsdId=" + record.id;
    var actionUrl = $.webPath() + "/jsgl/cgjsd/" + action;
    cgjsd_showDialog(title, href, actionUrl);
}

//弹出新增或修改的窗口
function cgjsd_showDialog(title, href, actionUrl) {
    var win = $.window({
        title: title,
        iconCls: 'icon-add2',
        width: 1024,
        height: 450,
        cache: false,
        url: href,
        modal: true,
        buttons: [
            {
                text: '保存',
                handler: function () {
                    $(win).mask();
                    $("#grid_cgjsdmx").edatagrid('saveRow');
                  /*  $("#grid_zjbcd").edatagrid('saveRow');*/
                    $('#form_cgjsd').form('submit', {
                        url: actionUrl,
                        onSubmit: function (param) {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            var cgdhm = $("#cgjsd_hm").val();
                            if (!cgdhm || cgdhm == '') {
                                $(win).mask('hide');
                                $.messager.alert("提示", "请填写发票号码!", "waring");
                                $("#form_cgjsd .tabs-title :eq(0)").click();
                                return false;
                            }
                            var data = $('#grid_cgjsdmx').edatagrid("getData");
                           /* var zjbcdata = $('#grid_zjbcd').edatagrid("getData");*/
                            if (!data.rows.length) {
                                $(win).mask('hide');
                                $('#form_cgjsd').tabs('select', "结算单明细");
                                $.messager.alert("提示", "请选择采购单!", "waring");
                                $("#form_cgjsd .tabs-title :eq(1)").click();
                                return false;
                            }

                            var arrayPrefix = 'cgjsd.cgdList';
                            var cgdmxPrefix = 'cgjsd.cgdmxList';
                         /*   var zjbcPrefix = 'cgjsd.zjbcdList';*/
                            var paramString = "{";

                            for (var i = 0; i < data.rows.length; i++) {
                                var objectString = $.toJSONStringSubPrefix(data.rows[i], arrayPrefix + "[" + i + "].", [
                                    ['cgd.id', 'id']
                                ]);
                                paramString = paramString + objectString.substring(1, objectString.length - 1) + ",";
                            }
                            for (var i = 0; i < data.rows.length; i++) {
                                var objectString = $.toJSONStringSubPrefix(data.rows[i], cgdmxPrefix + "[" + i + "].", [
                                    ['je', 'jsbhse'],
                                    ['se', 'jsse'],
                                    ['ce', 'jsce'],
                                    'id'
                                ]);
                                paramString = paramString + objectString.substring(1, objectString.length - 1) + ",";
                            }


                       /*     for (var i = 0; i < zjbcdata.rows.length; i++) {
                                {
                                    var objectString = $.toJSONStringSubPrefix(zjbcdata.rows[i], zjbcPrefix + "[" + i + "].", [
                                        'lb.id',
                                        'je',
                                        'cgdmx.id',
                                        'cgd.id'
                                    ]);
                                    paramString = paramString + objectString.substring(1, objectString.length - 1) + ",";
                                }
                            }*/

                            if (data.rows && data.rows.length > 0)
                                paramString = paramString.substring(0, paramString.length - 1);
                            paramString += "}";
                            $.extend(param, $.parseJSON(paramString));
                            return isValid;
                        },
                        success: function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_cgjsd').datagrid('reload');
                                $.messager.show({
                                    title: '提示',
                                    msg: json.message,
                                    timeout: 3000,
                                    showType: 'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

//删除采购结算单
function cgjsd_delete() {
    var row = $('#grid_cgjsd').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否删除该采购单结算？', function (r) {
            if (r) {
                var index = $('#grid_cgjsd').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/jsgl/cgjsd/delete_cgjsd",
                    dataType: 'json',
                    type: 'POST',
                    data: {cgjsdId: row.id},
                    beforeSend: function () {
                        $("#layout_cgjsd").mask();
                    },
                    success: function (data) {
                        $("#layout_cgjsd").mask("hide");
                        if (data.statusCode == 200) {
                            $('#grid_cgjsd').datagrid('deleteRow', index);
                            $.messager.show({
                                title: '提示',
                                msg: data.message,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        } else {
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要删除的采购单结算！", "warning");
    }
}

//审核
function cgjsd_check() {
    var row = $('#grid_cgjsd').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否对该采购结算单进行审核？', function (r) {
            if (r) {
                $.ajax({
                    url: $.webPath() + "/jsgl/cgjsd/change_status",
                    dataType: 'json',
                    type: 'POST',
                    data: {cgjsdId: row.id, zt: 2},
                    beforeSend: function () {
                        $("#layout_cgjsd").mask();
                    },
                    success: function (data) {
                        $("#layout_cgjsd").mask("hide");
                        if (data.statusCode == 200) {
                            $('#grid_cgjsd').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: data.message,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        } else {
                            $('#grid_cgjsd').datagrid('reload');
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要审核的采购单结算！", "warning");
    }
}

//采购结算单查询
function cgjsd_search() {
    var glqyId = $('#cgjsd_glqy_search').val() == null ? null : $('#cgjsd_glqy_search').val();
    var glqyQc = $('#cgjsd_glqy_search').attr("_text") == null ? null : $('#cgjsd_glqy_search').attr("_text");
    /* if (glqyQc == "") {
     glqyId = null;
     }*/
    var gsId = $('#cgjsdinto_gs').combotree('getValue') == null ? null : $('#cgjsdinto_gs').combotree('getValue');
    var gsJc = $('#cgjsdinto_gs').combotree('getText') == null ? null : $('#cgjsdinto_gs').combotree('getText');
    var fpzlId = $('#cgjsdinto_fpzl').combobox('getValue') == null ? null : $('#cgjsdinto_fpzl').combobox('getValue');
    var sqrqStart = $('#cgjsdinto_sqrq_start').datebox('getValue');
    var sqrqEnd = $('#cgjsdinto_sqrq_end').datebox('getValue');
    var zt = $('#cgjsdinto_zt').combobox('getValue') == null ? null : $('#cgjsdinto_zt').combobox('getValue');
    $('#grid_cgjsd').datagrid('load',
        {
            jsdh: $('#cgjsdJsdh').val(),
            glqyId: glqyId,
            glqyQc: glqyQc,
            gsId: gsId,
            gsJc: gsJc,
            fpzlId: fpzlId,
            sqrqStart: sqrqStart,
            sqrqEnd: sqrqEnd,
            zt: zt
        }
    );
}
